package com.yuntuzhilian.chapter4.demo9;

/**
 * @author MrWang-shuizhishuiheshui
 * @description
 */
public class GouPiao implements Runnable {
    //合计票数
    private int count=1000;
    //票号
    private int num=0;

    private boolean flag=false;

    @Override
    public void run() {
        while(!flag){
            sale();
        }
    }

    //添加线程锁,保证多线程情况下共享数据的信息统一(数据安全问题)
    public synchronized void sale(){
        if (count<=0){
            flag=true;
            return;
        }
        count--;
        num++;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"----抢到第"+num+"张票,还剩:"+count);
    }

}
